import tkinter
from CursorTool import CursorTool
from FunctionLimiter import desc_time
from ResourcePath import get_assets_dir
from TkWindow import TkWindow
from MoveWindow import (
    move_window_to_left_top,
    move_window_to_right_top,
    move_window_to_left_bottom,
    move_window_to_right_bottom,
    move_window,
)


FILE_NAME_CURSOR = get_assets_dir("target_3.ani")
FILE_NAME_CURSOR_W = get_assets_dir("target_4.ani")
ct = CursorTool(FILE_NAME_CURSOR, FILE_NAME_CURSOR_W)

tw = TkWindow()


def left_mouse_down_on_img(event):
    tw.imgLabel.configure(image=tw.img2)
    ct.set_new_cursor()


def left_mouse_up_on_mig(event):
    tw.imgLabel.configure(image=tw.img1)
    ct.restore_cursor()


@desc_time(0.03)  # 设置0.03时CPU占用1.6%，肉眼看不太卡，不设置时CPU占用5%
def moving_mouse_on_img(event):
    tw.update_info()
    ct.moving_mouse()


def left_mouse_down_on_button(event):
    hwnd = tw.listb.get(3, tkinter.END)[0][0]
    uo = tw.useroption.get()
    if uo == "左上角":
        move_window_to_left_top(hwnd)
    elif uo == "右上角":
        move_window_to_right_top(hwnd)
    elif uo == "左下角":
        move_window_to_left_bottom(hwnd)
    elif uo == "右下角":
        move_window_to_right_bottom(hwnd)
    elif uo == "自定义":
        x = tw.xentry.get()
        y = tw.yentry.get()
        if x == "" or x == "-":
            x = 0
        if y == "" or y == "-":
            y = 0
        x = int(x)
        y = int(y)
        move_window(hwnd, x, y)


tw.imgLabel.bind("<Button-1>", left_mouse_down_on_img)  # 鼠标左键按下
tw.imgLabel.bind("<ButtonRelease-1>", left_mouse_up_on_mig)  # 鼠标左键释放
tw.imgLabel.bind("<B1-Motion>", moving_mouse_on_img)  # 鼠标左键按下并移动
tw.button.bind("<Button-1>", left_mouse_down_on_button)  # 鼠标左键按下


tw.tk_mainloop()
